package com.bank.backend.service.impl;

import com.bank.backend.mapper.UserMapper;
import com.bank.backend.mapper.TradeMapper;
import com.bank.backend.model.entity.User;
import com.bank.backend.model.entity.Trade;
import com.bank.backend.model.vo.DashboardStatsVO;
import com.bank.backend.service.DashboardService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
public class DashboardServiceImpl implements DashboardService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private TradeMapper tradeMapper;

    @Override
    public DashboardStatsVO getStats() {
        DashboardStatsVO statsVO = new DashboardStatsVO();

        // 1. 获取活跃用户总数
        Long totalUsers = userMapper.selectCount(new LambdaQueryWrapper<User>()
                .eq(User::getIsDeleted, 0));
        statsVO.setTotalUsers(totalUsers);

        // 2. 获取今日交易笔数
        LocalDateTime startOfDay = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime endOfDay = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        Long todayTransactions = tradeMapper.selectCount(new LambdaQueryWrapper<Trade>()
                .ge(Trade::getTradeDate, startOfDay)
                .le(Trade::getTradeDate, endOfDay));
        statsVO.setTodayTransactions(todayTransactions);

        // 3. 系统状态 (暂时硬编码)
        statsVO.setSystemStatus("良好");

        return statsVO;
    }
} 